package com.water.camera.activity;

import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.plata.base.aclr.utils.SPHelper;
import com.water.camera.adapter.ColorAdapter;
import com.water.camera.adapter.EditListAdapter;
import com.water.camera.adapter.PinEditListAdapter;
import com.water.camera.bean.EditItem;
import com.water.camera.databinding.ActivityEditJigsawHeadBinding;
import com.water.camera.fragment.AddWordDlg;
import com.water.camera.fragment.BaseDialogFragment;
import com.water.camera.fragment.EditDlg;
import com.water.camera.fragment.EditPointDlg;
import com.water.camera.fragment.EditTimeDlg;
import com.water.camera.it.EditListener;

import java.util.ArrayList;

public class PinEditAct extends BaseActivity implements EditListener {

    ActivityEditJigsawHeadBinding binding;
    EditListAdapter adapter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityEditJigsawHeadBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        binding.ivBack.setOnClickListener(v -> finish());
        binding.tvSave.setOnClickListener(v -> finish());

        binding.rvTemplate.setLayoutManager(new LinearLayoutManager(this));

        ArrayList<EditItem> list = getIntent().getParcelableArrayListExtra("item");
        adapter = new PinEditListAdapter(list, this);
        binding.rvTemplate.setAdapter(adapter);

        binding.recycleviewColor.setLayoutManager(new LinearLayoutManager(this, RecyclerView.HORIZONTAL, false));
        int color = getIntent().getIntExtra("color", 0);
        binding.recycleviewColor.setAdapter(new ColorAdapter(this, color));
    }

    boolean change = false;

    @Override
    public void onShowChange(EditItem item) {
        change = true;
    }

    @Override
    public void onValueChange(EditItem item) {
        change = true;
    }

    public void toEdit(EditItem item, int p) {
        if (item.custom) {
            addWord(item, p);
            return;
        } else if (item.isTime) {
            EditTimeDlg dlg = new EditTimeDlg();
            dlg.setOnDismissListener(new BaseDialogFragment.OnDismissListener() {
                @Override
                public void onDismiss(boolean isOk, Object value) {
                    if (isOk) {
                        item.updateTime((Long) value);
                        adapter.notifyItemChanged(p);
                        onValueChange(item);
                    }
                }
            });
            dlg.show(getSupportFragmentManager(), "");
            return;
        } else if (item.key.contains("jingwei")) {
            EditPointDlg dlg = new EditPointDlg().now(item.key);
            dlg.setOnDismissListener(new BaseDialogFragment.OnDismissListener() {
                @Override
                public void onDismiss(boolean isOk, Object value) {
                    if (isOk) {
                        item.updatePoint(value.toString());
                        adapter.notifyItemChanged(p);
                        onValueChange(item);
                    }
                }
            });
            dlg.show(getSupportFragmentManager(), "");
            return;
        }
        EditDlg dlg = new EditDlg().setData(item);
        dlg.setOnDismissListener(new BaseDialogFragment.OnDismissListener() {
            @Override
            public void onDismiss(boolean isOk, Object value) {
                if (isOk && value instanceof EditItem) {
                    item.value = ((EditItem) value).value;
                    adapter.notifyItemChanged(p);
                    onValueChange(item);
                }
            }
        });
        dlg.show(getSupportFragmentManager(), "");
    }

    @Override
    public void onColorChange(int color) {
        change = true;
        String name = getIntent().getStringExtra("name");
        SPHelper.getInstance(this).putInt(name + "_color", color).commit();
    }

    @Override
    public void addWord() {
        addWord(null, 0);
    }

    private void addWord(EditItem item, int position) {
        String name = getIntent().getStringExtra("name");
        AddWordDlg dlg = new AddWordDlg().setKey(name, item);
        dlg.setOnDismissListener(new BaseDialogFragment.OnDismissListener() {
            @Override
            public void onDismiss(boolean isOk, Object value) {
                if (isOk) {
                    update(item, position, (EditItem) value);
                }
            }
        });
        dlg.show(getSupportFragmentManager(), "");
    }

    @Override
    public void finish() {
        if (change) {
            setResult(RESULT_OK);
        }
        super.finish();
    }

    protected void update(EditItem item, int position, EditItem obj) {
        if (item != null) {
            if (obj == null) {
                adapter.remove(position);
            } else {
                adapter.notifyItemChanged(position);
            }
        } else {
            adapter.append(obj);
        }
        onValueChange(item);
    }
}
